/*
 * @Author: 0x9DEFA478
 * @Date: 2025-06-21 21:13:32
 * @LastEditTime: 2025-08-11 22:45:57
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * 
 */
#include "./H_Unicode.h"




Hsize H_UTF8_ValidLength(const char* UTF8_Text,Hsize Length,int* IsError){
  Hbyte c;
  Hsize r;

  r=0;
  
  while(r<Length){
    c=(Hbyte)UTF8_Text[r];
    
    if((c&0x80U)==0x00U){
      r++;
      continue;
    }else{
      Hsize cnt=0;

      do{
        cnt++;
        c<<=1;
      }while((c&0x80U)!=0x00U);
      
      if(cnt<2){
        *IsError=1;
        return r;
      }
      
      if((r+cnt)>Length){
        *IsError=0;
        return r;
      }
      
      for(Hsize i=1;i<cnt;i++){
        if((((Hbyte)UTF8_Text[r+i])&0xC0U)!=0x80U){
          *IsError=1;
          return r;
        }
      }

      r+=cnt;
    }
  }

  *IsError=0;
  return r;
}



